import { defineStore } from 'pinia'
import request from '@/utils/request'
import { ApiRes } from '@/types/data'
import { Category, Banner, New, Hot } from '@/types/home'
import { topCategory } from '../constants'

const topCategoryList = topCategory.map(item => {
  return {
    name: item
  }
})
/**
 * 参数1：仓库的名字
 * 参数2：仓库的配置
 */
export default defineStore('home', {
  state: () => {
    return {
      // 分类列表
      // categoryList: topCategoryList as Category[], // 分类列表
      categoryList: [] as Category[], // 分类列表
      bannerList: [] as Banner[],
      newList: [] as New[],
      hotList: [] as Hot[]
    }
  },
  actions: {
    async getCategoryList () {
      // 接口地址：https://apipc-xiaotuxian-front.itheima.net/home/category/head
      // https://apipc-xiaotuxian-front.itheima.net/home/category/head
      // const res = await request.get<{ msg: string; result: Category[] }>('home/category/head')

      const res = await request.get<ApiRes<Category[]>>('home/category/head')

      // console.log(res)
      this.categoryList = res.data.result
    },
    async getBannerList () {
      const res = await request.get<ApiRes<Banner[]>>('home/banner')
      // console.log('bannerList', res)
      this.bannerList = res.data.result
    },

    async getNewList () {
      const res = await request.get<ApiRes<New[]>>('home/new')
      // console.log('getNewList', res)
      this.newList = res.data.result
    },
    async getHotList () {
      const res = await request.get<ApiRes<Hot[]>>('home/hot')
      // console.log('getHotList', res)
      this.hotList = res.data.result
    }
  }
})
